import 'dart:async';

import 'package:anx_reader/models/toc_item.dart';
import 'package:anx_reader/providers/book_toc.dart';
import 'package:anx_reader/providers/current_reading.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:langchain_core/tools.dart';

import 'base_tool.dart';

class CurrentBookTocTool extends RepositoryTool<JsonMap, Map<String, dynamic>> {
  CurrentBookTocTool(this._ref)
      : super(
          name: 'current_book_toc',
          description:
              'Gather the table of contents for the book the user is reading right now, including their current position. Use this when you need chapter structure or to navigate to a different section. Returns an isReading flag, the current location details, and the full hierarchical TOC with percentages.',
          inputJsonSchema: const {
            'type': 'object',
            'properties': <String, dynamic>{},
          },
          timeout: const Duration(seconds: 2),
        );

  final WidgetRef _ref;

  @override
  JsonMap parseInput(Map<String, dynamic> json) {
    return json;
  }

  @override
  Future<Map<String, dynamic>> run(JsonMap input) async {
    final readingState = _ref.read(currentReadingProvider);
    final tocItems = _ref.read(bookTocProvider);

    if (!readingState.isReading || readingState.book == null) {
      return {
        'isReading': false,
        'message':
            'No active reading session is detected. A table of contents is only available while reading.',
        'toc': <Map<String, dynamic>>[],
      };
    }

    final currentLocation = {
      'href': readingState.chapterHref,
      'title': readingState.chapterTitle,
      'percentage': readingState.percentage,
    };

    return {
      'isReading': true,
      'currentLocation': currentLocation,
      'toc': tocItems.map(_serializeTocItem).toList(),
    };
  }

  Map<String, dynamic> _serializeTocItem(TocItem item) {
    return {
      'id': item.id,
      'href': item.href,
      'title': item.label,
      'level': item.level,
      'formattedPercentage': item.percentage,
      'children': item.subitems.map(_serializeTocItem).toList(),
    };
  }
}

Tool currentBookTocTool(WidgetRef ref) {
  return CurrentBookTocTool(ref).tool;
}
